package com.example.h1nson.realphotography.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.asyncTask.LoginAsynTask;
import com.example.h1nson.realphotography.config.GetCurrentUser;
import com.example.h1nson.realphotography.entity.User;
import com.example.h1nson.realphotography.interface_photo.OnDataFinishedListener;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginActivity extends AppCompatActivity {
    private TextView textView;
    private EditText lgnAccountEt;
    private EditText lgnPwdEt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button btnLogin = (Button)findViewById(R.id.lgnSubBtn);
        textView = (TextView)findViewById(R.id.lgnTextView2) ;
        lgnAccountEt = (EditText)findViewById(R.id.lgnAccountEt) ;
        lgnPwdEt = (EditText)findViewById(R.id.lgnPwdEt);
        Bundle bundle = getIntent().getExtras();
        if(bundle!=null){
            String email = bundle.getString("email");
            lgnAccountEt.setText(email);
        }
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(checkInfo()) {
                   textView.setText("logining...");
                   User user = new User(lgnAccountEt.getText().toString(), lgnPwdEt.getText().toString());
                   LoginAsynTask loginAsynTask = new LoginAsynTask(user);
                   loginAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
                       @Override
                       public void onDataSuccessfully(Object data) {
                           if(data.toString().equals("success")){
                               textView.setText("登录成功，正在跳转");
                               Toast.makeText(LoginActivity.this,"登录成功成功",Toast.LENGTH_SHORT).show();
                               GetCurrentUser.userEmail = lgnAccountEt.getText().toString();
                               LoginSucces(0);
                           }else if(data.toString().equals("successWithMqtt")){
                               textView.setText("登录成功，正在跳转");
                               Toast.makeText(LoginActivity.this,"登录成功成功",Toast.LENGTH_SHORT).show();
                               GetCurrentUser.userEmail = lgnAccountEt.getText().toString();
                               LoginSucces(1);
                           }else if(data.toString().equals("emailNotExisted")){
                               textView.setText("用户邮箱不存在，请先注册");
                              // Toast.makeText(LoginActivity.this,"登录成功成功",Toast.LENGTH_SHORT).show();
                           }else if(data.toString().equals("wrongPassword")){
                               textView.setText("密码错误");
                              // Toast.makeText(LoginActivity.this,"登录成功成功",Toast.LENGTH_SHORT).show();
                           }else if(data.toString().equals("fail")){
                               textView.setText("登录异常，请重试");
                               // Toast.makeText(LoginActivity.this,"登录成功成功",Toast.LENGTH_SHORT).show();
                           }else {
                               textView.setText("异常，请重试");
                           }
                       }
                       @Override
                       public void onDataFailed() {
                           textView.setText("登录失败，请重试");
                       }
                   });
                   loginAsynTask.execute();
                   // TestHttpRequst.post(url, jsonStr);
//                read(url);
               }
            }
        });
    }
    private void LoginSucces(int i){
        Intent intent = new Intent();
        intent.setClass(LoginActivity.this, MainActivity.class);
        intent.putExtra("Email",lgnAccountEt.getText().toString());
        if(i==1){
            intent.putExtra("Mqtt",true);
        }else
            intent.putExtra("Mqtt",false);
        this.startActivity(intent);
    }
    public boolean checkInfo(){

        if(!checkEmail(lgnAccountEt)){
            return false;
        }
        if(!checkPwd(lgnPwdEt)){
            return false;
        }
        return true;

    }
    public boolean checkEmail(EditText edtEmail){
        String email = edtEmail.getText().toString();
        if(email==null){
            textView.setText("请输入邮箱");
            return false;
        }
        //正则表达式验证邮箱
        Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Matcher matcher = pattern.matcher(email);
        if(!matcher.matches()){
            textView.setText("无效邮箱，请重新输入邮箱");
        }
        return matcher.matches();
    }
    public boolean checkPwd(EditText edtPwd){
        String userPwd = edtPwd.getText().toString();
        if(userPwd==null){
            textView.setText("请输入密码");
            return false;
        }
        //正则表达式验证密码，密码6-20位字母，数字，数字字母组合
        Pattern pattern = Pattern.compile("^[0-9a-zA-Z]{6,20}$");
        Matcher matcher = pattern.matcher(userPwd);
        if(!matcher.matches()){
            textView.setText("无效密码，请重新输入密码（6-20位只允许字母、数字）");
            return false;
        }
        return matcher.matches();
    }
}
